<?php
namespace plugins\goods\controller;

use cmf\controller\ApiPluginBaseController;
use plugins\goods\model\GoodsModel;
use plugins\goods\model\GoodsShoppingTrolleyModel;
use plugins\goods\model\GoodsSpecAttrModel;
use plugins\goods\model\GoodsSpecListModel;
use plugins\member\model\MemberModel;
use plugins\store\model\StoreModel;

class ApiShoppingTrolleyController extends ApiPluginBaseController
{
    //购物车列表
    public function getShoppingTrolley()
    {
        $post                       = $this->request->post();
        $user                       = $this->getIdentity();
        $post['order']['member_id'] = $user->uid;
        $check                      = (new MemberModel)->get(['uid' => $user->uid]);
        if ($check) {
            //购物车的列表
            $list = (new GoodsShoppingTrolleyModel)->field('sku,num,store_id')->where(['uid' => $user->uid])->order('id', 'desc')->select()->toArray();
            $tmp  = [];
            //空的列表
            if (empty($list)) {
                return api_json_echo(true, '查询完成', $tmp);
            }
            foreach ($list as $key => $value) {
                $store = (new StoreModel)->get(['store_id' => $value['store_id']]);
                //店铺不存在的情况
                if ($store == null) {
                    continue;
                }
                $tmp = [];
                //查找店铺id作为key
                // $arr[$key]['store_name'] = $store->store_name;
                $arr[$value['store_id']]['store_name'] = $store->store_name;
                // $arr[$key]['store_id'] = $value['store_id'];
                $arr[$value['store_id']]['store_id'] = $store->store_id;
                $info['sku']                         = $value['sku'];
                $info['num']                         = (int) $value['num'];
                $info['goods_name']                  = '';
                $info['goods_price']                 = '';
                $info['spec_name']                   = '';
                $info['goods_id']                    = '';
                $info['goods_img']                   = '';
                $info['status']                      = true;
                //sku搜索商品信息
                $goods_spec = (new GoodsSpecListModel)->get(['sku' => $value['sku']]);
                //规格不存在
                if ($goods_spec == null) {
                    $info['status']                    = false;
                    $arr[$value['store_id']]['info'][] = $info;
                    continue;
                }
                //商品主体
                $good = (new GoodsModel)->get(['id' => $goods_spec->gid]);
                //商品主体不存在
                if ($good == null || $good->status == 0) {
                    $info['status']                    = false;
                    $arr[$value['store_id']]['info'][] = $info;
                    continue;
                }
                //商品信息有效
                if (!empty($goods_spec)) {
                    $good = (new GoodsModel)->get(['id' => $goods_spec->gid]);
                    if ($good == null) {
                        $info['status']                    = false;
                        $arr[$value['store_id']]['info'][] = $info;
                        // $arr[$key]['info'][] = $info;
                        continue;
                    }
                    $info['status'] = true;
                    //商品id
                    $info['goods_id'] = $good->id;
                    //中文名
                    $info['goods_name'] = $good->goods_name;
                    //价格
                    $info['goods_price'] = (int) $goods_spec->price;
                    //商品图片
                    if (!empty($goods_spec['img'])) {
                        $info['goods_img'] = zy_domain() . '/plugins/goods/view/image/' . $goods_spec->img;
                    } else {
                        $info['goods_img'] = $good->goods_img;
                    }
                    //规格转中文
                    $spec = explode('#*_*#', $goods_spec->attr);
                    foreach ($spec as $k => $v) {
                        $info['spec_name'] .= (new GoodsSpecAttrModel)->get(['attr_id' => $v])->attr_name . ',';
                    }
                    $info['spec_name'] = trim($info['spec_name'], ",");
                } else {
                    //商品信息无效
                    continue;
                }
                // $arr[$key]['info'][] = $info;
                //新增一条的商品信息
                $arr[$value['store_id']]['info'][] = $info;
            }
            if (empty($arr)) {
                return api_json_echo(true, '查询完成', $tmp);
            }
            foreach ($arr as $key => $value) {
                $tmp[] = $value;
            }
            return api_json_echo(true, '查询完成', $tmp);
        }
        return api_json_echo(false, '不存在的用户');
    }
    //添加商品
    public function addShoppingTrolley()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        //uid num sku
        $check = (new MemberModel)->get(['uid' => $info->uid]);
        if ($check) {
            $post['uid'] = $info->uid;
            //sku检查
            //检查sku有效性
            $sku_check = (new GoodsSpecListModel)->get(['sku' => $post['sku']]);
            if (!$sku_check) {
                return api_json_echo(false, '不存在的商品');
            }
            //检查购物车中是否已有商品
            $car_check        = (new GoodsShoppingTrolleyModel)->get(['sku' => $post['sku'], 'uid' => $info->uid]);
            $post['store_id'] = $sku_check->store_id;
            if ($car_check) {
                // $post['store_id'] = $sku_check->store_id;
                $post['num'] += $car_check->num;
                $car_check->allowField(true)->isUpdate(true)->save($post);
                return api_json_echo(true, '添加完成');
            }
            //无记录时加新
            $result = (new GoodsShoppingTrolleyModel)->allowField(true)->save($post);
            if ($result) {
                return api_json_echo(true, '添加完成');
            }
            return api_json_echo(false, '添加失败');
        }
        return api_json_echo(false, '不存在的用户');
    }
    //删除商品
    public function delShoppingTrolley()
    {
        $post  = $this->request->post();
        $info  = $this->getIdentity();
        $check = (new \plugins\member\model\MemberModel)->get(['uid' => $info->uid]);
        //传入 uid sku
        if ($check) {
            $list = explode(',', $post['sku']);
            foreach ($list as $key => $value) {
                $result = (new GoodsShoppingTrolleyModel)->destroy(['uid' => $info->uid, 'sku' => $value]);
            }
            // if($result){
            return api_json_echo(true, '删除完成');
            // }
            // return api_json_echo(false,'删除失败');
        }
        return api_json_echo(false, '不存在的用户');
    }
    //商品数量更改
    public function changeShoppingTrolley()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        //uid num sku
        $check = (new MemberModel)->get(['uid' => $info->uid]);
        if ($check) {
            //sku检查
            //检查sku有效性
            $sku_check = (new GoodsSpecListModel)->get(['sku' => $post['sku']]);
            if (!$sku_check) {
                return api_json_echo(false, '不存在的sku');
            }
            //检查购物车中是否已有商品
            $car_check = (new GoodsShoppingTrolleyModel)->get(['sku' => $post['sku'], 'uid' => $info->uid]);
            if ($car_check) {
                if ($post['num'] == 0) {
                    $car_check->destroy();
                    return api_json_echo(true, '已删除购物车中的商品');
                }
                $car_check->num = $post['num'];
                $car_check->save();
                return api_json_echo(true, '修改完成');
            }
            return api_json_echo(false, '购物车中无此商品');
        }
        return api_json_echo(false, '不存在的用户');
    }
}
